import Vue from 'vue'
import Router from 'vue-router'
import Login from '../views/Login'
import RoleManage from '../views/system/role/RoleManage'
import PermissionManage from '../views/system/permission/PermissionManage'
import UserManage from '../views/system/user/UserManage';
import DepartmentManage from '../views/system/department/DepartmentManage';
import DictManage from '../views/system/dict/DictManage';
import JobManage from '../views/schedule/job/JobManage'

import Home from '../views/SDSHome'

import persistent from '../common/persistentUtil'

Vue.use(Router)

let router = new Router({
    routes: [
        {
            path:'/',
            component:Home,
            children:[
                {
                    meta: {
                        breadcrumbName: ['系统管理', '角色管理'],
                        permission: 'role:manage'
                    },
                    path: 'system-roleManage',
                    component: RoleManage
                },
                {
                    meta: {
                        breadcrumbName: ['系统管理', '权限管理'],
                        permission: 'permission:manage'
                    },
                    path: 'system-permissionManage',
                    component: PermissionManage
                },
                {
                    meta: {
                        breadcrumbName: ['系统管理', '用户管理'],
                        permission: 'user:manage'
                    },
                    path: 'system-userManage',
                    component: UserManage
                },
                {
                    meta: {
                        breadcrumbName: ['系统管理', '部门管理'],
                        permission: 'department:manage'
                    },
                    path: 'system-departmentManage',
                    component: DepartmentManage
                },
                {
                    meta: {
                        breadcrumbName: ['系统管理', '字典管理'],
                        permission: 'dict:manage'
                    },
                    path: 'system-dictManage',
                    component: DictManage
                },
                {
                    meta: {
                        breadcrumbName: ['任务调度', '定时任务'],
                        permission: 'schedule:manage'
                    },
                    path: 'schedule-jobManage',
                    component: JobManage
                }
            ]
        },

        {
            path: '/login',
            name: 'login',
            component: Login
        }
    ]
});


router.beforeEach((to, from, next) => {




    let flag = false;
    //跳过这两个路由
    if (to.path != '/' && to.path != '/login' && to.path != Vue.prototype.$loginPath) {


        let token = persistent.getToken();

        if(token == null || token == undefined){

            router.replace("/login")
            return;
        }
        let tp = to.meta.permission;

        if (tp) {

            let permissions = persistent.getPermissions();

            if (permissions.indexOf(tp) > -1) {
                flag = true;
            }
        }else{
            flag = true;
        }
    }
    else if(to.path == '/login' || to.path == Vue.prototype.$loginPath){

        let token = persistent.getToken();

        if(token != null && token != undefined){

            router.replace("/")

        }else{
            next();

        }
        return;
    }
    else {
        flag = true;
    }


    if (!flag) {

        //权限有问题
        //TODO 是否要跳转到其他页面
        console.log("当前登录用户无权限访问:" + to.path);

    } else {
        next();
    }

});

export default router;